<?php namespace app\common\model;

use think\Loader;
use think\Validate;
use houdunwang\arr\Arr;
use think\db;
//标签模型
class Tag extends Base{

	//添加标签
	public function addData($data){
		
		$validate = Loader::validate('Tag');
		if(!$validate->check($data)){
		    return ['valid'=>0,'msg'=>$validate->getError()];
		}else{
//			halt($data);
			//移除字符串两侧的空白字符或其他预定..
			$tname=trim($data['tname']);
			//以其他字符替换字符串中的一些字符（区分大小写）。
			$find=array(',','，','.','\\','-','*','、');
			$datas=str_replace($find,'、',$tname);
			//把字符串转化为数组
			$new=explode('、',$datas);
			foreach($new as $k=>$v){
				   $tname=[
				      'tname'=>$v,
				      'sort'=>$data['sort']
				   ];
				$num=db('tag')->insert($tname);
				
			}
              if($num){
              	   return ['valid'=>1,'msg'=>'标签添加成功！'];
              }else{
              	    return ['valid'=>0,'msg'=>'标签添加失败！'];
              }
//			
			
		}
		
	}
	//编辑标签
	public function editData($data){
	   $validate = Loader::validate('Tag');
		if(!$validate->check($data)){
		    return ['valid'=>0,'msg'=>$validate->getError()];
		}else{
//			halt($data);
           $tname=trim($data['tname']);
			$num=db('Tag')
			    ->where('tid',$data['tid'])
			    ->update(['tname'=>$tname,'sort'=>$data['sort']]);
            if($num){
            	return ['valid'=>1,'msg'=>'标签修改成功！'];
            }else{
            	return ['valid'=>0,'msg'=>'标签修改失败！'];
            }
			 
		}
		
		
	}
	
}
